package com.example.yrdev.ordering_system.Menu_Setting;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.yrdev.ordering_system.Common.CommonActivity;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by luke on 11/8/16.
 */

public class SettingLanguageActivity extends CommonActivity {
      @InjectView(R.id.lang_list)ListView lang_list;



    String[]lanKey;
    interface itemClickListener{
        void keySelected(int pos);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private  int getKeyIndex(){
        String currentKey=GlobalFunction.getCurrentLanKey(this);
        GlobalFunction.printTextByTag(currentKey);

        ArrayList arrayList=new ArrayList<>(Arrays.asList(lanKey));

        return arrayList.indexOf(currentKey);


    }
    @Override
    public void initView() {
        super.initView();


        setBarTitle(getResources().getString(R.string.language));
        setContentView(R.layout.setting_language_layout);
        ButterKnife.inject(this);
        showBackButton();

       lanKey=getResources().getStringArray(R.array.lanKey);


        String[]languages={"中文","English"};
        CustomAdapter customAdapter=new CustomAdapter(this,languages);

        lang_list.setAdapter(customAdapter);

        GlobalFunction.printTextByTag("cc"+String.valueOf(getKeyIndex()));

        customAdapter.setItemClickListener(new itemClickListener() {
            @Override
            public void keySelected(int pos) {
                String key=lanKey[pos];

                initLanguage(key);
                GlobalFunction.setLanKey(SettingLanguageActivity.this,key);

                restart();
            }
        });



    }


   private class CustomAdapter extends BaseAdapter implements View.OnClickListener{

       String[] lanList;
        LayoutInflater inflater;
       itemClickListener itemClickListener;
    Context context;

       CustomAdapter(Context context,String[] lanList){
            this.context=context;
           inflater=LayoutInflater.from(context);
           this.lanList=lanList;

      }

       public void setItemClickListener(SettingLanguageActivity.itemClickListener itemClickListener) {
           this.itemClickListener = itemClickListener;
       }



       @Override
       public Object getItem(int i) {
           return lanList[i];

       }

       @Override
       public long getItemId(int i) {
           return 0;
       }


       private  int getKeyIndex(){
           String currentKey=GlobalFunction.getCurrentLanKey(context);
           int index=0;
           for (int i=0;i<lanKey.length;i++){
               String temp=lanKey[i];
               if (temp.equals(currentKey)){
                   index=i;

                   break;
               }
           }


           return index;


       }

       @Override
       public int getCount() {
           return lanList.length;
       }
        int post;

       @Override
       public View getView(int i, View view, ViewGroup viewGroup) {
            if (view==null){
                view=inflater.inflate(R.layout.lang_row_layout,null);
                view.setId(i);
            }

           TextView lanName=(TextView)view.findViewById(R.id.lang_name);
           lanName.setText((String)getItem(i));
            view.setOnClickListener(this);

            post=getKeyIndex();




           if (post==i){
             //  GlobalFunction.printTextByTag(String.valueOf(view.getId())+":"+String.valueOf(post));
               view.setSelected(true);
               lanName.setTextColor(ContextCompat.getColor(context,R.color.mainColor));
               view.setBackground(ContextCompat.getDrawable(context,R.drawable.lan_row_pressed));

           }

           else{

               view.setSelected(false);
               lanName.setTextColor(ContextCompat.getColor(context,R.color.text_color_hint));
               view.setBackground(ContextCompat.getDrawable(context,R.drawable.lan_row_normal));

           }


           return view;

       }

       @Override
       public void onClick(View view) {
  //         post=view.getId();
//           notifyDataSetChanged();

           notifyDataSetChanged();
         //  view.setSelected(true);
        if (itemClickListener!=null)
            itemClickListener.keySelected(view.getId());

       }
   }



}
